package com.ec.system.modules.sys.controller;

import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.core.common.core.base.BasePageVO;
import com.ec.core.mybatis.groups.AddGroup;
import com.ec.core.mybatis.groups.OtherGroup;
import com.ec.core.mybatis.groups.UpdateGroup;
import com.ec.core.secure.annotations.PreAuth;
import com.ec.system.modules.sys.dto.QueryRoleDTO;
import com.ec.system.modules.sys.dto.SysRoleDTO;
import com.ec.system.modules.sys.service.ISysRoleService;
import com.ec.system.modules.sys.vo.SysRoleVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 角色接口
 * @author: Eric Lee
 * @time: 2021/8/6 15:13
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {
    @Resource
    private ISysRoleService sysRoleService;


    /**
     * 获取角色列表
     *
     * @param listRoleDTO
     * @return
     */
    @GetMapping("/list")
    @PreAuth("sys_role_view,sys_user_add,sys_user_edit")
    public Result<BasePageVO<SysRoleVO>> listRole(QueryRoleDTO listRoleDTO) {
        return ResponseMsg.success(sysRoleService.listRole(listRoleDTO));
    }

    /**
     * 添加菜单
     *
     * @param sysRoleDTO
     * @return
     */
    @PostMapping
    @PreAuth("sys_role_add")
    public Result<Void> addRole(@Validated(AddGroup.class) @RequestBody SysRoleDTO sysRoleDTO) {
        return ResponseMsg.status(sysRoleService.addRole(sysRoleDTO));
    }

    /**
     * 更新操作
     *
     * @param sysRoleDTO
     * @return
     */
    @PutMapping
    @PreAuth("sys_role_edit")
    public Result<Void> updateRole(@Validated(UpdateGroup.class) @RequestBody SysRoleDTO sysRoleDTO) {
        return ResponseMsg.status(sysRoleService.updateRole(sysRoleDTO));
    }

    /**
     * 删除操作
     *
     * @return
     */
    @DeleteMapping("/{roleId}")
    @PreAuth("sys_role_delete")
    public Result<Void> deleteRole(@PathVariable long roleId) {
        SysRoleDTO sysRoleDTO = new SysRoleDTO();
//        sysRoleDTO.setMenuIds(menuIds);
        sysRoleDTO.setRoleId(roleId);
        return ResponseMsg.status(sysRoleService.deleteRole(sysRoleDTO));
    }

    /**
     * 配置角色权限
     *
     * @param sysRoleDTO
     * @return
     */
    @PostMapping("/configRolePer")
    @PreAuth("sys_role_config_per")
    public Result<Void> configRolePer(@Validated(OtherGroup.class) @RequestBody SysRoleDTO sysRoleDTO) {
        return ResponseMsg.status(sysRoleService.configRolePer(sysRoleDTO));
    }
}
